﻿using System;
using System.Collections.Generic;

using System.Text;
using System.Data.SqlClient;
using System.Data;
using Entity;

namespace Data
{
    public class Service_Data
    {
        SqlConnection cnn;
        SqlCommand comm;
        SqlDataAdapter da;
        DataSet ds;


        public DataTable Binding()
        {
            cnn = Connection.StartConnection();
            comm = new SqlCommand("BindingServicePrice", cnn);
            comm.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(comm);
            ds = new DataSet();
            try
            {
                da.Fill(ds, "serviceprice");
                return ds.Tables["serviceprice"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                ds.Dispose();
                da.Dispose();
                cnn.Close();
                cnn.Dispose();
            }
        }

        public bool AddService(Service_Entity serviceprice)
        {
            try
            {
                cnn = Connection.StartConnection();
                comm = new SqlCommand("AddServicePrice", cnn);
                cnn.Open();
                comm.CommandType = CommandType.StoredProcedure;
                comm.Parameters.Add(new SqlParameter("@sername", serviceprice.SerName));
                comm.Parameters.Add(new SqlParameter("@serprice", serviceprice.SerPrice));
                comm.ExecuteNonQuery();
                cnn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Connection.CloseConnection();
            }
            return true;
        }


        public int UpdateService(int serID, string sername, int serprice)
        {
            cnn = Connection.StartConnection();
            cnn.Open();
            comm = new SqlCommand("UpdateServicePrice", cnn);
            comm.CommandType = CommandType.StoredProcedure;
            try
            {
                comm.Parameters.AddWithValue("@serID", serID);
                comm.Parameters.AddWithValue("@sername", sername);
                comm.Parameters.AddWithValue("@serprice", serprice);
                return comm.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            finally
            {
                Connection.CloseConnection();
            }       
        }

        public int DeleteService(int id)
        {
            cnn = Connection.StartConnection();
            cnn.Open();
            comm = new SqlCommand("DeleteServicePrice", cnn);
            comm.CommandType = CommandType.StoredProcedure;
            try
            {
                comm.Parameters.AddWithValue("@serID", id);
                return comm.ExecuteNonQuery();

            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                comm.Dispose();
                cnn.Close();
                cnn.Dispose();
            }
        }
    }
}
